'''
1.data什么时候要bytes()
2.':authority': 'cart.jd.com',本来头参数带：的，记得要去掉才是正确的，变成这样'authority': 'cart.jd.com',



'''


from requests.exceptions import RequestException
from bs4 import BeautifulSoup
import json
import time
import requests
import random


def login():

    login_url = 'https://passport.jd.com/uc/loginService'

    data = {
        'uuid': '216800ab-33d2-4294-8a79-8c4b4bb9ed86',
        'eid': 'XZJQG5BJ2JLLFPRRATLAX5GNFVUW2VM7YFNHWKF2CAALSM7GPMUER5NQJTHA4D3YDNL3SC2LOPANQEQ7SCJN7Y3LWQ',
        'fp': 'da0bddd0c91c764ef8f679c0b9451a01',
        '_t': '_t',
        'loginType': 'c',
        'loginname': '13632635022',
        'nloginpwd': 'mCNPufNXb6NwJ+Di41J/nml8M/Z+Cj7IgTn5EdpNIHKhL5C6w3A/dPfc+KBtx5jjsKNCjQDLmQTd6UN+616I2zGqZBULrCrStPxaIH3wfZwlJMdft0YQoDxSxdkAhnsg9+Dmtzu/45IVGA0jZNr1BsiQyCcCw64QxZC0i9NAZd8=',
        'authcode': '3e130d22d60e4e9099e50fd3cc7594c1',
        'pubKey': 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB',
        'sa_token': 'B68C442BE645754F33277E701208059080DD726A94A73F76DEC3053A838549C06EB7D3797CE1C5BBE7C2B2EF9CA7D467C3C76FF0A28885EE64B432120BA9B13D348C69B7D2A54084AD0AF9F604987E3FF4F05CFA833594DEAA638A1460132F8E4FC41F9984A0550F77FF3A51047D9FFA6937B2323ADE6CDB3A98776094AD46AFC0D104BE5A33FD4B2D219E65930F424CA62E9A76B0553DAFABE006ED3256B130266A76FAF5CCE3ADAB479A1B91EBE72C2D610ABDE7440C44A7E5A7EBA8CDD0028D4843B5BDB2C55E152856356F6B240C888009363507A9AF700992BF6E904210AE231FAB2EE17DB02CC3CB76D286CC11655280634422D319305808A90D97CDCD4CBFC8F4144AB1065C5E3E9B48C8C89D731251333ABCDB30E460BB92B9875F66484C8CBA330281C0E6D767027F04FBBE28BD4650695A2394700A95F2CD739D0AE2880CC4814F486CCA671A0692BD70F3E622E8475222B4B03BB16FAB6A62F0D19921F52CD62A82B304069B10B160E6E0174666067833EFB42447840A1381393523806AD3975C00D6A5BEEF57964EB28ED8949103A5C46B6F94FBF552C5E6907D18AC931BEEE19EF8DA095E07CC72177EACC440123B1B676E59279EB489714D65F625F5C5DCAD066DAD68B987FF17B21A244570E6079C6C137A196575199EFB720F9BB581F4015960B0C308EFDA432EF34C8596734A2C832E1112B59311E76D95858A880AFE2919287C37A151159FEFC776A5D798D977CEDFF171E9B1B5FEF05E318496F34AEDAAE048887C63B90BF9B47355DC345F69573E6CD7335ED75A985285053D9FBED49A8426D6A6958D65BE5E',
        'seqSid': '4873113220605236679',
        'useSlideAuthCode': '1',
    }

    params = {
        'nr': '1',
        'uuid': '216800ab-33d2-4294-8a79-8c4b4bb9ed86',
        'clstag1': 'login|keycount|5|5',
        'clstag2': 'login|keycount|5|6',
        'r': '0.2763087206443111',
        't': '1550967185442',
        'r': '0.6575352196536206',
        'version': '2015',
    }

    headers = {
        'Accept': 'text/plain, */*; q=0.01',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Origin': 'https://passport.jd.com',
        'Referer': 'https://passport.jd.com/uc/popupLogin2013?clstag1=login|keycount|5|5&clstag2=login|keycount|5|6&r=0.2763087206443111&t=1550967185442',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
        'X-k39sGHmW-a': 'SmvpJZ0_B9vkO_z9S_K5ztTPddIqamoPT9K6hPBhSth5nlN-ran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UePVfB9qpxNduzE5fTsKFm9Yc7gh_zuL1MIw9JfcBS9-9MWW1JPv6JmyjnOWNSrqgaVcPiDZWfyzWQudrtt85WNVWMVqj_E8_W=Hqnl5UaZ8UhsvkB65Ix=1KoYBNrv8ymtoNMWWkSu7Pet5W7IBP37h64=hIS=o-S9h5Juprx=cc7dE14y56',
        'X-k39sGHmW-b': '-qm8ezx',
        'X-k39sGHmW-c': 'A_MO-4hlAQAA29XveTX0-KoQbPiy3xbjIijWum-rT-EHQtuHV6skyxlsJAsDAawUIfCucinBwH8AAOfvAAAAAA==',
        'X-k39sGHmW-d': '0',
        'X-k39sGHmW-uniqueStateKey': 'A7ae-4hlAQAAngSz4VMCAl-Vlq71Ta_smPn9uLwC7A7bioCLfwLMadcFYPb2AawUIfGLr4YoFOgAAAAAAAAAAA==',
        'X-Requested-With': 'XMLHttpRequest',
    }
    try:
        res = s.post(login_url, headers=headers, data=data, params=params)
        # 原本是res=requests.post(login_url, headers=headers, data=bytes(data, encoding='UTF-8'), params=params),用处于开启状态的cookie管理器s打开url地址并获取返回对象，相当于赋予了这个返回对象获得cookie的能力，不然cookie写不了在这个返回对象身上
        if res.status_code == 200:
            return True
        else:
            return None
    except RequestException:
        return None


def get_cartPage():
    headers = {
        'authority': 'cart.jd.com',
        'method': 'GET',
        'path': '/cart?rd=0.04706494837589137',
        'scheme': 'https',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'accept-encoding': 'deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,fr;q=0.7',
        'cache-control': 'max-age=0',
        'cookie': 'shshshfpa=01b3bd95-8b3b-e47d-1f4c-8c1f80abc0fd-1547348698; __jdu=15473486976611003950221; PCSYCityID=1684; shshshfpb=iwagd07a4KtnjA1vzQdMN9Q%3D%3D; ipLoc-djd=1-72-4137-0; areaId=1; unpl=V2_ZzNtbRBVExdyWE5ULB8LVWJXEl1LV0cUIg9PVy4RWlE0UBENclRCFX0UR1dnGlwUZAIZX0VcQRFFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHsdWQNlBhtaSlFEEnUIR1V5EVgEZAASbXJQcyVFDUVXcxhcNWYzE20AAx8VfA9AVHtUXAFiBRBYS1BLE3IPRlR6GF4NYwIRXkJnQiV2; __jdv=122270672|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_c3a36a91f7fa4e118150f682d97ebb2a|1550752594061; user-key=68e8fa52-7d1a-4e0f-b36a-e17456e79f39; cd=0; _pst=jd_672d1cea82fb8; unick=jd_136326ocym; pin=jd_672d1cea82fb8; _tp=g3%2B0SPZN8DZmQpcS6RoRc6AqTHQ3JxM%2FBfB9iwkffrc%3D; pinId=hPol3MihFQxGJrMERjbwqbV9-x-f3wj7; cart-main=xx; shshshfp=5af4d4a80ccf82c460a12de5fffefc2c; __jda=122270672.15473486976611003950221.1547348698.1550915670.1550967177.8; __jdc=122270672; wlfstk_smdl=h10fsn7cbhsnwk8ibsk4xccermhl144i; TrackID=1jNA2l5g5IsQ_rcCCR0_TCw7adWmLnqdTcy4dgY5XBvzQVeX5CL9pESgjNONuscPs745ATt_ktJXLG8a_QlaEHrn6sVJX12VAw8MSsZy51Z8; thor=FE352A13D993F56E68003867F502A935B715286B652084B6EADC9E060E987FECB9D9BFFBA302BBB4A6D5E3BC985F1BFD41C0B158E2333DC459374A265C0AFFE4D06CFACA1DF89F5EA0E1F336442D3928D2D065A1FFA7C424137F0FA2E0899319DD7ECDE3C6977E22289EBCB887A8A5AA1CA3A618D9B7D17241AE5A2E11870C3D85525E67F2F32560C3809E8B799B3A98460F0EB4722EAC19BC9F6F840E8A35CB; ceshi3.com=000; cn=4; 3AB9D23F7A4B3C9B=FRTZONRQ4PFKQC6MO367CIWWCQZPQ2OC73XRXTN6KMF6CXBWBRYEBY4JSNGJYHHIIXMRS3SXWYTTKNYBSG2VA7OSCM',
        'referer': 'https://cart.jd.com/cart.action',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
    }
    url = 'https://cart.jd.com/cart?rd=0.04706494837589137'
    res = s.get(url, headers=headers)
    try:
        if res.status_code == 200:
            return res.content.decode('UTF-8')
        else:
            return None
    except RequestException:
        return None


def paresPage(item):
    # 获取bs返回对象，别忘了指定解析器
    soup = BeautifulSoup(item, 'lxml')
    # 获取到所有商品信息<div class="item-form">....</div>
    items = soup.find_all(name='div', attrs={"class": 'item-form'})
    # 遍历取出每一部商品信息并进行封装
    for item in items:
        yield{
            'name': item.find(name='div', attrs={'class': 'p-name'}).find(name='a').string.strip(),
            'brief': item.find(name='div', attrs={'class': "props-txt"}).get_text(),
            'price': item.find(name='strong').get_text(),
        }


def writeFile(content):
    '''解析爬取网页中内容，并返回结果'''
    with open("./京东bs4.txt", 'a', encoding="utf-8") as f:
        f.write(json.dumps(content, ensure_ascii=False) + "\n")
        # 编码中文默认也是ascii编码，所以记得指定ensur_ascii=False


def main():
    if login():
        print('登陆中······')
        time.sleep(2)
        time.sleep(random.randint(2, 5))
        content = get_cartPage()
        for item in paresPage(content):
            print(item)
            writeFile(item)
    else:
        print('Shit')


if __name__ == '__main__':
    # 返回一个可以维持状态的Session
    # 相当于处于打开状态的cookie管理器
    s = requests.Session()
    main()
    time.sleep(random.randint(2, 5))
